# reads configuration file and store options

import ConfigParser
CONFIG_FILE = 'cfg.txt'
cp = ConfigParser.ConfigParser()
# loads and reads CONFIG_FILE
cp.read(CONFIG_FILE)

try:
    zoom_inv = eval(cp.get('ui', 'zoom inversion'))
except:
    zoom_inv = False

print 'Zoom inversion: ', zoom_inv


class ReadConfig:
    def __init__(self, filepath):
        file = open(filepath)
        file_lines = file.read().split('\n')
        file.close()
        
        for line in file_lines:
            # do this only if the line isn't empty
            if line != '':
                linea = remove_tabs(line)
                
                option_name, value = linea.split('\t')
                
                # create a new var that store the option value
                exec 'self.' + option_name + ' = ' + value

# remove spaces and replace them with tabulations
def remove_tabs(line):
    last = ''
    linea = ''
    for c in line:
        if c == ' ':
            if last != ' ':
                linea += '\t'
        else:
            linea += c
                    
        last = c
    
    return linea

# reads list of values in a file
def read_values(filepath):
    file = open(filepath)
    file_lines = file.read().split('\n')
    file.close()
    
    list = []
    
    for line in file_lines:
        # do this only if the line isn't empty
        if line != '':
            linea = remove_tabs(line)
            
            list.append(linea.split('\t'))
    
    return list
